# -*- coding: utf-8 -*-
"""
# 使用了 [python-docx==1.2.0]，遵循其 [MIT] 许可证，原始代码来源：[https://python-docx.readthedocs.io/en/latest/]
# 使用了 [pandas==2.2.2]，遵循其 [BSD 3-Clause License] 许可证，原始代码来源：[https://pandas.pydata.org]
"""

import sys
import pandas as pd
import shutil
import os
from docx import Document
import datetime

样板文档路径=r"E:\E_Python\f_in\附件3. xxx见书.docx"

def 读取表格信息(fp,header):
    df=pd.read_excel(fp,header=header,dtype=str)
    # print(df.columns.values.tolist())
    # ['序号', 'xx', 'xx称', 'xxx名称'']

    序号列表=df["序号"].values.tolist()
    项目列表=df["研发项目名称"].values.tolist()
    xxx.

    for 序号,项目,... in \
     zip(序号列表,项目列表,...):
        目标文件名=f"{序号}.docx"
        目标文件=os.path.join(r"E:\E_Python\f_out",目标文件名)
        shutil.copy2(样板文档路径,目标文件)

        doc=Document(目标文件)

        cnt_t=0
        cnt_r=0
        cnt_c=0
        cnt_p=0
        cnt_run=0
        set_cell=[]

        c_p=0
        c_r=0
        for para in doc.paragraphs:
            c_p=c_p+1
            c_r=0
            if len(para.runs)!=0:
                for run in para.runs:
                    c_r=c_r+1
                    print("段落:%s,段%d,节%d"%(run.text,c_p,c_r))
                    if c_p==2 and c_r==4:
                        run.text=run.text.replace(run.text,f":{str(序号).strip()})")
                        break
            else:
                print("内容:%s,段%d"%(run.text,c_p))

        for table in doc.tables:
            cnt_t=cnt_t+1
            cnt_r=0
            if len(table.rows)!=0:
                for row in table.rows:#for rows.
                    cnt_r=cnt_r+1
                    cnt_c=0
                    if len(row.cells)!=0:
                        for cell in row.cells:#for cells.
                            cnt_c=cnt_c+1
                            cnt_p=0
                            if len(cell.paragraphs)!=0:
                                for para in cell.paragraphs:#for paragraphs.
                                    cnt_p=cnt_p+1
                                    cnt_run=0
                                    if cnt_t==1 and cnt_r==1 and cnt_c==2 and cnt_p==1:#项目名称.
                                        para.text=para.text.replace(para.text,str(项目).strip())
                                        continue
                                    if cnt_t==1 and cnt_r==1 and cnt_c==4 and cnt_p==1:#项目编号.
                                        # para.text=para.text.replace(para.text,str(序号).strip())
                                        continue
                                    if cnt_t==1 and cnt_r==2 and cnt_c==5 and cnt_p==1:#
                                        para.text=para.text.replace(para.text,str(xx).strip())
                                        continue
                                    if cnt_t==1 and cnt_r==3 and cnt_c==3 and cnt_p==4:#高新技术领域.
                                        if str(xx).strip() in ["电子信息","生物与新医药","航空航天","新材料","高技术服务","新能源与节能","资源与环境","先进制造与自动化"]:
                                            para.text=para.text.replace(para.text,str(xx).strip())
                                            continue
                                    if cnt_t==1 and cnt_r==3 and cnt_c==3 and cnt_p==7:#
                                        if xx==xx:
                                            para.text=para.text.replace(para.text,str(xxx).strip())
                                            continue
                                    if len(para.runs)!=0:
                                        for run in para.runs:#for runs.
                                            cnt_run=cnt_run+1
                                            print("内容:%s,表%d,行%d,格%d,段%d,节%d"%(run.text,cnt_t,cnt_r,cnt_c,cnt_p,cnt_run))
                                            if cnt_t==1 and cnt_r==2 and cnt_c==1 and cnt_p==1 and cnt_run==1:#项目起始时间
                                                try:
                                                    start_time=datetime.datetime.strptime(str(启动),"%Y-%m-%d %H:%M:%S")
                                                    year=start_time.strftime("%Y")
                                                    month=start_time.strftime("%m")
                                                    day=start_time.strftime("%d")
                                                    t_out=f"{year}年{month}月{day}日 至"
                                                    run.text=run.text.replace(run.text,str(t_out).strip())
                                                except:
                                                    pass
                                                continue
                                            if cnt_t==1 and cnt_r==2 and cnt_c==1 and cnt_p==1 and cnt_run==3:#项目结束时间
                                                try:
                                                    stop_time=datetime.datetime.strptime(str(结束),"%Y-%m-%d %H:%M:%S")
                                                    year=stop_time.strftime("%Y")
                                                    month=stop_time.strftime("%m")
                                                    day=stop_time.strftime("%d")
                                                    t_out=f"{year}年{month}月{day}日"
                                                    run.text=run.text.replace(run.text,str(t_out).strip())
                                                except:
                                                    pass
                                                continue
                                            if cnt_t==1 and cnt_r==3 and cnt_c==3 and cnt_p==2 and cnt_run==1:#类型
                                                if str(xx类型).strip()=="xxx":
                                                    run.text=run.text.replace(r"□",r"√")
                                                continue
                                            if cnt_t==1 and cnt_r==3 and cnt_c==3 and cnt_p==3 and cnt_run==2:#
                                                if str(xx).strip() in ["电子信息","生物与新医药","航空航天","新材料","高技术服务","新能源与节能","资源与环境","先进制造与自动化"]:
                                                    run.text=run.text.replace(r"□",r"√")
                                                continue
                                            if cnt_t==1 and cnt_r==3 and cnt_c==3 and cnt_p==3 and cnt_run==4:#
                                                if (str(领域).strip())=="否":
                                                    run.text=run.text.replace(r"□",r"√")
                                                continue
                                            if cnt_t==1 and cnt_r==4 and cnt_c==2 and cnt_p==2 and cnt_run==2:#是否xx
                                                if str(是否xx).strip()=="属于":
                                                    run.text=run.text.replace(r"       ",r"属于")
                                                elif str(是否xx).strip()=="不属于":
                                                    run.text=run.text.replace(r"       ",r"不属于")
                                                continue
                                            if cnt_t==1 and cnt_r==4 and cnt_c==2 and cnt_p==2 and cnt_run==4:#是否xx
                                                if str(是否xx).strip()=="适用":
                                                    run.text=run.text.replace(r"       ",r"适用")
                                                elif str(是否xx).strip()=="不适用":
                                                    run.text=run.text.replace(r"       ",r"不适用")
                                                continue
                                            if cnt_t==1 and cnt_r==4 and cnt_c==2 and cnt_p==3 and cnt_run==1:#否定理由
                                                if str(是否xx).strip()=="属于":
                                                    run.text=run.text.replace(run.text,r"")
                                                    continue
                                            if cnt_t==1 and cnt_r==4 and cnt_c==2 and cnt_p==4 and cnt_run==1:#否定理由
                                                run.text=run.text.replace(run.text,r"")
                                            if cnt_t==1 and cnt_r==4 and cnt_c==2 and cnt_p==6 and cnt_run==1:#否定理由
                                                if str(是否xx).strip()=="属于":
                                                    run.text=run.text.replace(run.text,r"")
                                                    continue
                                                elif str(是否xx).strip()=="不属于":
                                                    deny_reason=r""
                                                    if 否因1==否因1:
                                                        deny_reason+=str(否因1).strip()
                                                        if 否因2==否因2:
                                                            deny_reason+="；"
                                                        # else:
                                                            # deny_reason+="。"
                                                    if 否因2==否因2:
                                                        deny_reason+=str(否因2).strip()
                                                        if 否因3==否因3:
                                                            deny_reason+="；"
                                                        # else:
                                                            # deny_reason+="。"
                                                    if 否因3==否因3:
                                                        deny_reason+=str(否因3).strip()
                                                        # deny_reason+="。"
                                                    if(len(deny_reason)>0):
                                                        run.text=run.text.replace(run.text,deny_reason)
                                                    else:
                                                        run.text=run.text.replace(run.text,r"无")
                                                    continue
                                            if cnt_t==1 and cnt_r==4 and cnt_c==2 and cnt_p==6 and cnt_run==2:#否定理由
                                                if str(是否xx).strip()=="属于":
                                                    run.text=run.text.replace(run.text,r"")
                                                    continue
                                    else:
                                        print("内容:%s,表%d,行%d,格%d,段%d"%(para.text,cnt_t,cnt_r,cnt_c,cnt_p))
                            else:
                                print("内容:%s,表%d,行%d,格%d"%(cell.text,cnt_t,cnt_r,cnt_c))
                    else:
                        print("内容:%s,表%d,行%d"%(row.text,cnt_t,cnt_r))
            else:
                print("内容:%s,表%d"%(table.text,cnt_t))
        doc.save(目标文件)


if __name__ == "__main__":
    fp=r"E:\E_Python\result\py_20250723_114515.xlsx"
    读取表格信息(fp,header=0)
